package com.superpai.view.custom;

import android.content.Context;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.Gallery;
import android.widget.ImageView;

/**
 * @author Devin.Hu
 * @date 2011-9-17
 * @version V1.0
 * @description 自定义MirrorGallery类，实现镜像倒影效果
 */
public class MirrorGallery extends Gallery {

	private Camera mCamera = new Camera();
	private float mMaxRotationAngle = 45;
	private int mMaxZoom = -1500;
	private int baseScale = 1300;
	private int mCoveflowCenter;
	
	public MirrorGallery(Context context) {
		super(context);
		this.setStaticTransformationsEnabled(true);
	}
	
	public MirrorGallery(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.setStaticTransformationsEnabled(true);
	}
	
	public MirrorGallery(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.setStaticTransformationsEnabled(true);
	}
	
	/**
	 * 得到画廊的中心位置
	 *
	 * @return
	 */
	private int getCenterOfCoverflow() {
		return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2
				+ getPaddingLeft();
	}
	
	/**
	 * 得到View的中心位置
	 *
	 * @param view
	 * @return
	 */
	private static int getCenterOfView(View view) {
		return view.getLeft() + view.getWidth() / 2;
	}
	
	/**
	 *	画廊滚动时的特效
	 *	
	 * @param child
	 * @param t
	 * @return
	 */
	@Override
	protected boolean getChildStaticTransformation(View child, Transformation t) {
		
		final int childCenter = getCenterOfView(child);
		final int childWidth = child.getWidth();
		float rotationAngle = 0;
		t.clear();
		t.setTransformationType(Transformation.TYPE_MATRIX);
		if (childCenter == mCoveflowCenter) {
			
			transformImageBitmap((ImageView) child, t, 1, rotationAngle);
		} else {
			float scale = (mCoveflowCenter - childCenter) / (float)this.getWidth() ;
			
			rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);   
            if (Math.abs(rotationAngle) > mMaxRotationAngle) {   
                rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle   
                        : mMaxRotationAngle;
            }

            transformImageBitmap((ImageView) child, t, scale, rotationAngle);
		}

		return true;
	}
	
	 public float getInterpolation(float input, float factor) {
	          return (float)(1.0f - Math.pow((1.0f - input), 2 * factor));
	 }
	 
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		mCoveflowCenter = getCenterOfCoverflow();
		super.onSizeChanged(w, h, oldw, oldh);
	}

	/**
	 * 处理图片效果
	 *
	 * @param child
	 * @param t
	 * @param scale
	 * @param rotationAngle
	 * @return 
	 */
	private void transformImageBitmap(ImageView child, Transformation t,
			float scale, float rotationAngle) {
		
		mCamera.save();
		final Matrix imageMatrix = t.getMatrix();
		final int imageHeight = child.getLayoutParams().height;
		final int imageWidth = child.getLayoutParams().width;
		mCamera.translate(0.0f, 0.0f, baseScale);
		float scaleX = Math.abs(scale);
		if(scaleX<0.1){
			scaleX = 0.1f;
		}
		
		if (scaleX < 1) {
			mCamera.translate((1-scaleX)*rotationAngle, 0, (1-scaleX)*mMaxZoom);
			
			child.setAlpha((int) (255 - Math.abs(scale * 220)));
			
		}else if(scaleX==1){
			mCamera.translate(0, 0, mMaxZoom*0.9f );
		}
		
		mCamera.rotateY(rotationAngle);
		mCamera.getMatrix(imageMatrix);
		float translateRote = 0.5f;
		imageMatrix.preTranslate(-(int)(imageWidth * translateRote), -(int)(imageHeight * translateRote));
		imageMatrix.postTranslate((int)(imageWidth * translateRote), (int)(imageHeight * translateRote));
		mCamera.restore();
	}
	
	/**
	 *  返回绘制顺序
	 * 
	 * @param childCount
	 * @param i
	 * @return 
	 */
	@Override
	protected int getChildDrawingOrder(int childCount, int i) {
		
		int selectPosition = this.computeHorizontalScrollOffset() - this.getFirstVisiblePosition();
		
		if (selectPosition < 0) return i;
        
        if (i == childCount - 1) {
            return selectPosition;
        } else if (i >= selectPosition) {
        	return childCount - 1 + selectPosition - i;
        } else {
            return i;
        }
	}
}
